﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RMCharGen.Views
{
    public partial class StatisticsView : UserControl
    {
        private const string NullCharacterError = "The Character property has not been set.";

        private bool _allowEdits = false;

        public StatisticsView()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Provides the numeric only values in the editing cells
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StatisticsGrid_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (this.StatisticsGrid.CurrentCell.ColumnIndex == 1 ||
                this.StatisticsGrid.CurrentCell.ColumnIndex == 2)
            {
                if (e.Control is TextBox)
                {
                    TextBox editControl = e.Control as TextBox;
                    editControl.KeyPress -= new KeyPressEventHandler(NumericCell_KeyPress);
                    editControl.KeyPress += new KeyPressEventHandler(NumericCell_KeyPress);
                    editControl.MaxLength = 3;
                }
            }
        }

        private void StatisticsGrid_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 1)
            {
                Model.StatisticType statType = DetermineStatistic(e.RowIndex);
                this.Character.Statistics[statType].Actual = (int)StatisticsGrid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
                Bind();
            }
        }

        /// <summary>
        /// Prevents non numeric keys being entered into the cell
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void NumericCell_KeyPress(object sender,
                                  KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar))
            {
                Keys key = (Keys)e.KeyChar;
                if ((!(key == Keys.Back ||
                      key == Keys.Delete)) ||
                      e.KeyChar == '.')
                {
                    e.Handled = true;
                }
            }
        }

    }
}
